﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using BLL;
using System.Collections;

public partial class BrowserDatas : System.Web.UI.Page
{

    
    private  Operation op = new Operation();
    private static DataSet ds = new DataSet();
    private static int iNowPage = 1;
    private static int iCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        GetPower();
        if (!IsPostBack)
        {
            ds = op.GetInformation();
            DoBind();
            lbUser.Text = Session["User"].ToString();
            lbRole.Text = Session["Role"].ToString();
        }
    }

    //为GridView绑定数据
    private void DoBind()
    {
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
        iCount = ds.Tables[0].Rows.Count;
        lbDataCount.Text = iCount.ToString();
        if (iCount % 5 == 0)
        {
            iCount = iCount / 5;
        }
        else
        {
            iCount = iCount / 5 + 1;
        }
        ddlGoTo.Items.Clear();
        for (int i = 1; i <= iCount; i++)
        {
            ddlGoTo.Items.Add(i.ToString());
        }
        lbCount.Text = iCount.ToString();            
    }

    //获取用户权限
    private void GetPower()
    {
        DataSet ds = (DataSet)Session["Power"];
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            switch (row[0].ToString())
            {
                case "删除": GridView1.Columns[10].Visible = true; break;
                case "添加需求": LinkButton1.Visible = true; break;
                case "上传": LinkButton2.Visible = true;break;
                case "管理": LinkButton5.Visible = true; break;
                default:
                    break;
            }
        }        
    }

    //数据绑定方法
    private void BindData(int nowpage)
    {            
        DataTable dt = new DataTable();
        dt = ds.Tables[0].Copy();
        dt.Clear();
        for (int i = 5 * (nowpage - 1); i < 5 * nowpage; i++)
        {
            DataRow row = dt.NewRow();
            try
            {
                for (int j = 0; j < 10; j++)
                {
                    row[j] = ds.Tables[0].Rows[i][j];
                }
                dt.Rows.Add(row);
            }
            catch
            {                  
                break;
            }
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();       
    }

    
    //显示全部
    protected void btnDispalyAll_Click(object sender, EventArgs e)
    {
        ds = op.GetInformation();
        iNowPage = 1;
        DoBind();
        BindData(iNowPage);
    }

    //模糊查询
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        ds = op.Query(tbCondition.Text);
        iNowPage = 1;
        DoBind();
        BindData(iNowPage);
    }

    //删除数据行
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {    
        int id = 0;

        // Append the values of the key fields to the log text.
        foreach (DictionaryEntry keyEntry in e.Keys)
        {
            id = Convert.ToInt32(keyEntry.Value);
        }

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (Convert.ToInt32(ds.Tables[0].Rows[i]["id"]) == id)
            {
                ds.Tables[0].Rows[i].Delete();
            }
        }
    }

    //下一页
    protected void lbNextPage_Click(object sender, EventArgs e)
    {
        if (iNowPage == iCount)
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('已经是尾页！')", true);
            return;
        }
        iNowPage++;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //上一页
    protected void lbLastPage_Click(object sender, EventArgs e)
    {       
        if (iNowPage == 1) 
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('已经是首页！')", true);
            return;
        }
        iNowPage--;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //首页
    protected void lbFirstPage_Click(object sender, EventArgs e)
    {
        iNowPage = 1;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //尾页
    protected void lbEndPage_Click(object sender, EventArgs e)
    {
        iNowPage = iCount;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //跳转
    protected void ddlGoTo_SelectedIndexChanged(object sender, EventArgs e)
    {
        iNowPage = int.Parse(ddlGoTo.SelectedItem.Value);
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //提交需求按钮
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        bool flag = false;
        DataSet ds = (DataSet)Session["Power"];
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i][0].ToString() == "添加需求")
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            Response.Redirect(@"~\PresentDemand.aspx");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('没有添加需求权限！')", true);
            return;
        }
    }

    //上传按钮
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        bool flag = false;
        DataSet ds = (DataSet)Session["Power"];
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i][0].ToString() == "上传")
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            Response.Redirect(@"~\UpLoadFile.aspx");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('没有上传权限！')", true);
            return;
        }
    }

    //退出按钮
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Session.Clear();
        iNowPage = 1;
        Response.Redirect(@"~\UserLogin.aspx");
    }

    //权限管理按钮
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        bool flag = false;
        DataSet ds = (DataSet)Session["Power"];
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i][0].ToString() == "管理")
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            Response.Redirect(@"~\Management.aspx");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('没有管理权限！')", true);
            return;
        }
    }
}